package com.deryun.tyl.bean.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.deryun.tyl.bean.User;

public interface UserMapper {
	
	@Select({
		"SELECT",
		"*",
		"FROM user where username = #{0,jdbcType=VARCHAR}"
	})
	@ResultMap("BaseResultMap")
	public User getUserByUsername(String username);
	
	@Select({
		"select * from `user` where username = #{0,jdbcType=VARCHAR} AND `password` = #{1,jdbcType=VARCHAR}"
	})
	@ResultMap("BaseResultMap")
	User doLogin(String username,String password);
	
	@Insert({
		"INSERT INTO `user` (institution_name,username,password,contact_man,"
		+ "contact_phone,email,date,role,institution_type,institution_code,institution_pic_path,"
		+ "institution_account_name,institution_bank,institution_account_num,"
		+ "supplier_fax,provider_other_zone,institution_onekey_auth,institution_bank_auth,account_auth,institution_auth"
		+ ") VALUES ("
		+ "#{institutionName,jdbcType=VARCHAR},"
		+ "#{username,jdbcType=VARCHAR},"
		+ "#{password,jdbcType=VARCHAR},"
		+ "#{contactMan,jdbcType=VARCHAR},"
		+ "#{contactPhone,jdbcType=VARCHAR},"
		+ "#{email,jdbcType=VARCHAR},"
		+ "#{date,jdbcType=VARCHAR},"
		+ "#{role,jdbcType=VARCHAR},"
		+ "#{institutionType,jdbcType=VARCHAR},"
		+ "#{institutionCode,jdbcType=VARCHAR},"
		+ "#{institutionPicPath,jdbcType=VARCHAR},"
		+ "#{institutionAccountName,jdbcType=VARCHAR},"
		+ "#{institutionBank,jdbcType=VARCHAR},"
		+ "#{institutionAccountNum,jdbcType=VARCHAR},"
		+ "#{supplierFax,jdbcType=VARCHAR},"
		+ "#{providerOtherZone,jdbcType=VARCHAR},"
		+ "#{institutionOnekeyAuth,jdbcType=VARCHAR},"
		+ "#{institutionBankAuth,jdbcType=VARCHAR},"
		+ "#{accountAuth,jdbcType=VARCHAR},"
		+ "#{institutionAuth,jdbcType=VARCHAR}"
		+ ")"
	})
	@Options(useGeneratedKeys = true, keyProperty = "user.id")
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", resultType=int.class, before = true)
	int addUser(User user);
	
	@Select({
		"select * from `user` where username = #{0,jdbcType=VARCHAR} OR institution_name = #{1,jdbcType=VARCHAR}"
	})
	@ResultMap("BaseResultMap")
	User validateUser(String username, String institutionName);
	
	@Select({
		"SELECT `user`.id FROM `user` ORDER BY id DESC LIMIT 1,1 "
	})
	int getNewId();
	

	
	//机构认证
	@Update({
		"update `user` set "
		+ "institution_name = #{institutionName,jdbcType=VARCHAR},"
		+ "institution_type = #{institutionType,jdbcType=VARCHAR},"
		+ "institution_code = #{institutionCode,jdbcType=VARCHAR},"
		+ "institution_pic_path = #{institutionPicPath,jdbcType=VARCHAR},"
		+ "institution_account_name = #{institutionAccountName,jdbcType=VARCHAR},"
		+ "institution_bank = #{institutionBank,jdbcType=VARCHAR},"
		+ "institution_account_num = #{institutionAccountNum,jdbcType=VARCHAR},"
		+ "supplier_fax = #{supplierFax,jdbcType=VARCHAR},"
		+ "provider_other_zone = #{providerOtherZone,jdbcType=VARCHAR},"
		+ "institution_auth = '1' "
		+ "where id = #{id,jdbcType=INTEGER}"
	})
	
	int authInstitution(User tuser);
	
	//一键通认证
	@Update({
		"update government.user set "
		+ "institution_onekey_auth = '1'"
		+ "where id = #{0,jdbcType=VARCHAR}"
	})
	int authOneKey(String id);
	
	//银行认证
	@Update({
		"update government.user set "
		+ "institution_bank_auth = '1'"
		+ "where id = #{0,jdbcType=VARCHAR}"
	})
	int authBank(String id);
	
	@Select({
		"SELECT * FROM `user`"
	})
	@ResultMap("BaseResultMap")
	List<User> getAllUser();
	
	@Select({
		"SELECT * FROM `user` WHERE id = #{0,jdbcType=INTEGER}"
	})
	@ResultMap("BaseResultMap")
	User getUserById(int id);
	
	@Update({
		"UPDATE `user` SET "
		+ "id_card_num = #{idCardNum,jdbcType=VARCHAR},"
		+ "card_pic_front = #{cardPicFront,jdbcType=VARCHAR},"
		+ "card_pic_back = #{cardPicBack,jdbcType=VARCHAR},"
		+ "head_pic = #{headPic,jdbcType=VARCHAR},"
		+ "password = #{password,jdbcType=VARCHAR},"
		+ "contact_man = #{contactMan,jdbcType=VARCHAR},"
		+ "contact_phone = #{contactPhone,jdbcType=VARCHAR}"
		+ "WHERE "
		+ "id = #{id,jdbcType=VARCHAR}"
	})
	int modifyUser(User user);
	

	List<User> searchUser(String role,String institutionName);
	
	@Update({
		"UPDATE `user` SET "
		+ "account_auth = #{accountAuth,jdbcType=VARCHAR},"
		+ "institution_auth = #{institutionAuth,jdbcType=VARCHAR},"
		+ "institution_onekey_auth = #{institutionOnekeyAuth,jdbcType=VARCHAR},"
		+ "institution_bank_auth = #{institutionBankAuth,jdbcType=VARCHAR}"
		+ "WHERE "
		+ "id = #{id,jdbcType=VARCHAR}"
	})
	int accessAuth(User user);
	
	List<User> searchAuth(String auth,String institutionName);
	
	@Update({
		"UPDATE `user` u SET u.show_at_front = '1' WHERE id = #{0,jdbcType=INTEGER}"
	})
	int toFrontSupplier(int id);
	
	@Update({
		"UPDATE `user` u SET u.show_at_onekey = '1' WHERE id = #{0,jdbcType=INTEGER}"
	})
	int toOneKeySupplier(int id);
	
	@Update({
		"UPDATE `user` u SET u.show_at_front = '0' WHERE id = #{0,jdbcType=INTEGER}"
	})
	int removeFrontSupplier(int id);
	
	@Update({
		"UPDATE `user` u SET u.show_at_onekey = '0' WHERE id = #{0,jdbcType=INTEGER}"
	})
	int removeOneKeySupplier(int id);
	
	@Select({
		"SELECT count(*) FROM `user` WHERE show_at_front = 1"
	})
	int getCountFront();
	
	@Select({
		"SELECT count(*) FROM `user` WHERE show_at_onekey = 1"
	})
	int getCountOneKey();
	
	@Select({
		"SELECT * FROM `user` WHERE show_at_front = '1'"
	})
	@ResultMap("BaseResultMap")
	List<User> getFrontSupplier();
	
	@Select({
		"SELECT * FROM `user` WHERE show_at_onekey = '1'"
	})
	@ResultMap("BaseResultMap")
	List<User> getOneKeySupplier();
}
